package cn.eeepay.boss.action;

import cn.eeepay.boss.system.DataSource;
import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.MerTerChangeBind;
import cn.eeepay.framework.model.Result;
import cn.eeepay.framework.model.TerminalInfo;
import cn.eeepay.framework.service.MerTerChangeBindService;
import cn.eeepay.framework.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author rpc
 * @date 2020-08-27
 */
@RestController
@RequestMapping("/merTerChangeBind")
public class MerTerChangeBindAction {

    private static final Logger log = LoggerFactory.getLogger(MerTerChangeBindAction.class);

    @Resource
    private MerTerChangeBindService merTerChangeBindService;


    /**
     * 条件查询
     *
     * @param merTerChangeBind
     * @return
     */
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping("/selectMerTerChangeBindPage")
    public Result selectMerTerChangeBindPage(@RequestBody MerTerChangeBind merTerChangeBind,
                                             @RequestParam(defaultValue = "1") int pageNo,
                                             @RequestParam(defaultValue = "10") int pageSize) {
        Result result = new Result();
        try {
            log.info("查询条件{}", merTerChangeBind);
            Page<MerTerChangeBind> page = new Page<>(pageNo, pageSize);
            merTerChangeBindService.selectMerTerChangeBindPage(page, merTerChangeBind);
            Map<String, Object> msg = new HashMap<>();
            msg.put("page", page);
            result.setStatus(true);
            result.setMsg("查询成功");
            result.setData(msg);
        } catch (Exception e) {
            result = ResponseUtil.buildResult(e);
            log.error("条件查询异常", e);
        }
        return result;
    }

    @RequestMapping("/toUpperBind/{id}")
    @SystemLog(description = "上游绑定", operCode = "merTerChangeBind.toUpperBind")
    public Result toUpperBind(@PathVariable String id) {
        Result result = new Result();
        try {

            MerTerChangeBind merTerChangeBind = merTerChangeBindService.selectMerTerChangeBindById(id);
            if (merTerChangeBind == null) {
                result.setMsg("不存在的记录");
                return result;
            }
            if (!"2".equals(merTerChangeBind.getChangeStatus())) {
                result.setMsg("此状态无需上游绑定");
                return result;
            }

            TerminalInfo terminalInfo = merTerChangeBindService.selectBySnAndMerNo(
                    merTerChangeBind.getOldSn(), merTerChangeBind.getMerchantNo());
            if (terminalInfo == null) {

                //查询没有绑定记录做此操作
                merTerChangeBind.setChangeStatus("3");
                merTerChangeBind.setRebindRemark("该机具已经解绑");
                merTerChangeBindService.updateChangeStatus(merTerChangeBind);

                result.setMsg("该机具已经解绑");
                return result;
            }
            //进行上游绑定操作
            result = merTerChangeBindService.toBindingTerminal(merTerChangeBind);

        } catch (Exception e) {
            result = ResponseUtil.buildResult(e);
            log.error("上游绑定异常", e);
        }
        return result;
    }

}
